﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RegistrationManagement.Models;

namespace RegistrationManagement.Controllers
{
    public class RegistrationController : Controller
    {
        private RegistrationEntities db = new RegistrationEntities();

        //
        // GET: /Registration/

        public ViewResult Index()
        {
            return View(db.Registrations.Include(r => r.ContactInfo).Include(r => r.ContactInfo.Company).ToList());
        }

        //
        // GET: /Registration/Details/5

        public ViewResult Details(int id)
        {
            Registration registrationmodel = db.Registrations.Find(id);
            return View(registrationmodel);
        }

        //
        // GET: /Registration/Create
        [Authorize]
        public ActionResult Create()
        {
            var contracts =
                db.Contracts
                .AsEnumerable()
                .Select(co => new SelectListItem()
                                    {
                                        Text = co.Name,
                                        Value = co.ContractId.ToString(CultureInfo.InvariantCulture)
                                    });
            var contacts =
                db.Contacts
                .AsEnumerable()
                .Select(c => new SelectListItem()
                                            {
                                                Text = c.Name,
                                                Value = c.ContactId.ToString(CultureInfo.InvariantCulture)
                                            }
                        );


            var vm = new RegEditViewModel() {Contacts = contacts, Contracts = contracts};


            return View(vm);
        }

        //
        // POST: /Registration/Create

        [HttpPost]
        [Authorize]
        public ActionResult Create(RegEditViewModel registrationmodel)
        {
            if (ModelState.IsValid)
            {
                db.Registrations.Add(registrationmodel.Registration);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(registrationmodel);
        }

        //
        // GET: /Registration/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            var regEditViewModel = new RegEditViewModel();
            var reg = db.Registrations.Find(id);
            regEditViewModel.Registration = reg;
            regEditViewModel.Contacts = db.Contacts.Include(c => c.Company)
                .AsEnumerable()
                .Select(c => new SelectListItem()
                            {
                                Selected = c.ContactId == reg.ContactId,
                                Text = c.Name + (c.Company == null ? string.Empty : "(" + c.Company.Name + ")"),
                                Value = c.ContactId.ToString()
                            });
            return View(regEditViewModel);
        }

        //
        // POST: /Registration/Edit/5

        [HttpPost]
        [Authorize]
        public ActionResult Edit(RegEditViewModel registrationmodel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(registrationmodel.Registration).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(registrationmodel);
        }

        //
        // GET: /Registration/Delete/5

        public ActionResult Delete(int id)
        {
            Registration registrationmodel = db.Registrations.Find(id);
            return View(registrationmodel);
        }

        //
        // POST: /Registration/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Registration registrationmodel = db.Registrations.Find(id);
            db.Registrations.Remove(registrationmodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Search(string q)
        {
            var res = db.Registrations.Include(r => r.ContactInfo).Where(r => r.HardwardId.Contains(q)).ToList();
            return PartialView(res);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}